import request from '@/utils/axios'

// 系统配置搜索请求参数
export interface SystemSettingSearchReq {
  id?: string        // 配置ID
  groupName?: string // 分组名称
  key?: string       // 配置键
  pageIndex: number  // 页码
  pageSize: number   // 每页大小
}

// 系统配置数据
export interface SystemSettingDto {
  id: string
  groupName: string
  key: string
  value: string
  description: string
  createTime: string
  updateTime: string
}

// 系统配置结果数据
export interface SystemSettingResult {
  id: string
  groupName: string
  key: string
  value: string
  description: string
  createTime: string
  updateTime: string
  enable: boolean | null
  modifyTime: string
}

// 分页响应数据
export interface SystemSettingPageRes {
  hasNextPage: boolean
  list: SystemSettingResult[]
  nextPage: number
  count: number
  total: number
  size: number
}

// 新增配置请求参数
export interface SettingDto {
  groupName: string    // 配置组名称
  description?: string // 描述（可选）
  key: string          // 配置项Key
  value: string        // 配置项Value
  enable?: boolean     // 是否生效（可选）
}

// API接口定义
const Api = {
  SystemSettingPage: '/setting/page',
  UpdateEnableStatus: '/setting',
  SaveSetting: '/setting',
  UpdateSetting: '/setting',
  DeleteSetting: '/setting',
  ConfigOss: '/configuration/oss-sts'
} as const

// 获取系统配置分页列表
export const getSystemSettingPageApi = (params: SystemSettingSearchReq) => {
  return request.get<SystemSettingPageRes>(Api.SystemSettingPage, { params })
}

// 更新配置启用状态
export const updateEnableStatusApi = (id: string, enable: boolean) => {
  return request.post(`${Api.UpdateEnableStatus}/${id}/enable`, null, {
    params: { enable }
  })
}

// 新增配置
export const saveSettingApi = (data: SettingDto) => {
  return request.post(Api.SaveSetting, data)
}

// 编辑配置
export const updateSettingApi = (id: string, data: SettingDto) => {
  return request.put(`${Api.UpdateSetting}/${id}`, data)
}

// 删除配置
export const deleteSettingApi = (id: string) => {
  return request.post(`${Api.DeleteSetting}/${id}/remove`)
}

// 获取配置
export const getSystemConfigOss = () => {
  return request.get(`${Api.ConfigOss}`)
}